package com.ucar.ui.pay;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Random;

public class PayUtils {
	
	public static final String PARTNER = "2088111268353505";
	public static final String SELLER = "sg_happy@sina.com";
	public static final String RSA_PRIVATE = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKX9YzS3170xGL2gP69j1cqaN67I7DlR/nrZHj/lHmuvejPgqYtqzDXsuCSc6n0ZG3yq6UQy2WxLedWXor49utW+ueYI/lS+/UGWctP+XO/tR3Q8xEUp2dp3YkF6eP4x5tVLC+FdNbXkqNFxP/AOBD80SpqvgZyXSGxxs5m0+DTZAgMBAAECgYAIbdWb17oc4WFgP8L8JRW2NB6fwZQ9VOyXUG84z2m8Di0mdUIm0kWMd3jiYxWFRMEgk41E6VAhL7QAJLMe9gOGwi8sMDASus8yB+FJqBb56A9LgwY1zwKco0KoXmqTMhwSq/xS85MDrHpx8qxv28+yWDuqXsTYNX2/i1yOlVAAAQJBANsMJ+nNR5t1seDlnG5bVOssxgvRqWyMcMLHSGHyWNVHGt+vN2Qq9Th+SlthpKcrTwKDN2OIOgrR1pbk+oyyO2ECQQDB/d4ryXE5iQkC1bbXV5sVFrag0LK49lzyMrGg6jbY/OYbcHAibeMsT5JiTN51pp5ajBxXHIped23E9AK0sKR5AkEApvhpJEsaHmJkqO3dhDGSVYHXpQK4hsm22oJVpnO5SzncPk2th2lHazy5KVcQz2yv2VZ0TiZ1N/IkBPmeEsl5gQJACsuU1Hz2RwxgKv83mI4wfCDycsqKV46mJ3g4uEqwWKziJT7BjrjsvmeDLuM2Mffesg0/+n0FGjKtMhueFu4JiQJAOr7Nn0aAu++47ytce7rJ94OIeHUIX6iKklYV0x/FFMR/qjHYaslDlOXFmViWtiE6C8JDL+x06aKK2p7wHSAgOQ==";
	public static final String RSA_PUBLIC = "";

	public static String getPayInfo(String subject, String body, String price, String tradeNo) {
		String orderInfo = getOrderInfo(subject, body, price, tradeNo);
		String sign = sign(orderInfo);
		try {
			// 仅需对sign 做URL编码
			sign = URLEncoder.encode(sign, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		final String payInfo = orderInfo + "&sign=\"" + sign + "\"&"
				+ getSignType();
		return payInfo;
	}

	/**
	 * create the order info. 创建订单信息
	 * 
	 */
	public static String getOrderInfo(String subject, String body, String price, String outTradeNo) {
		// 合作者身份ID
		String orderInfo = "partner=" + "\"" + PARTNER + "\"";

		// 卖家支付宝账号
		orderInfo += "&seller_id=" + "\"" + SELLER + "\"";

		// 商户网站唯一订单号
		orderInfo += "&out_trade_no=" + "\"" + outTradeNo + "\"";

		// 商品名称
		orderInfo += "&subject=" + "\"" + subject + "\"";

		// 商品详情
		orderInfo += "&body=" + "\"" + body + "\"";

		// 商品金额
		orderInfo += "&total_fee=" + "\"" + price + "\"";

		// 服务器异步通知页面路径
		orderInfo += "&notify_url=" + "\"" + "http://www.tdaoj.com/alipayJsp/notify_app_url.jsp"//"http://www.tdaoj.com/alipayJsp/notify_url.jsp"//"http://notify.msp.hk/notify.htm"
				+ "\"";

		// 接口名称， 固定值
		orderInfo += "&service=\"mobile.securitypay.pay\"";

		// 支付类型， 固定值
		orderInfo += "&payment_type=\"1\"";

		// 参数编码， 固定值
		orderInfo += "&_input_charset=\"utf-8\"";

		// 设置未付款交易的超时时间
		// 默认30分钟，一旦超时，该笔交易就会自动被关闭。
		// 取值范围：1m～15d。
		// m-分钟，h-小时，d-天，1c-当天（无论交易何时创建，都在0点关闭）。
		// 该参数数值不接受小数点，如1.5h，可转换为90m。
		orderInfo += "&it_b_pay=\"30m\"";

		// 支付宝处理完请求后，当前页面跳转到商户指定页面的路径，可空
		orderInfo += "&return_url=\"m.alipay.com\"";

		// 调用银行卡支付，需配置此参数，参与签名， 固定值
		// orderInfo += "&paymethod=\"expressGateway\"";

		return orderInfo;
	}

	/**
	 * get the out_trade_no for an order. 获取外部订单号
	 * 
	 */
	public static String getOutTradeNo() {
		SimpleDateFormat format = new SimpleDateFormat("MMddHHmmss",
				Locale.getDefault());
		Date date = new Date();
		String key = format.format(date);

		Random r = new Random();
		key = key + r.nextInt();
		key = key.substring(0, 15);
		return key;
	}

	/**
	 * sign the order info. 对订单信息进行签名
	 * 
	 * @param content
	 *            待签名订单信息
	 */
	public static String sign(String content) {
		return SignUtils.sign(content, RSA_PRIVATE);
	}

	/**
	 * get the sign type we use. 获取签名方式
	 * 
	 */
	public static String getSignType() {
		return "sign_type=\"RSA\"";
	}

}
